using Framework.Bundle;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEditor;
using UnityEngine;

/**
从 BuildSettings.txt 读回项目设置参数
主要参数包括
    BuildTarget
    BuildTargetGroup
    companyName
    productName
    Identifier
    bundleVersion
    bundleVersionCode
    ScriptingBackend
    AndroidArchitecture
    ScriptingDefines
    keystoreFile
    keystorePass
    keyaliasName
    keyaliasPass
    Development
    BuildScenes

从 InputSettings.txt 读回输入参数 

主要参数包括
    BuildBundle:bool 是否打包资源
    BuildProject:bool 是否导出工程
    override:bool 是否覆盖设置
    PathExportBundle:string 资源生成路径
    PathExportProject:strin 项目生成路径
    

**/
namespace Framework
{
    public partial class BuildCommand
    {
        private static BuildCommandContext context = new BuildCommandContext();
        private static List<IBuildCommandProcessor> _cmds = new List<IBuildCommandProcessor>();
        private static bool _pringArgs;
        private static void getProcessors()
        {
            _cmds.Clear();
            var typeList = Utils.Assembly.GetEditorTypes(typeof(IBuildCommandProcessor));
            foreach (var item in typeList)
            {
                if (System.Activator.CreateInstance(item) is IBuildCommandProcessor cmd)
                {
                    _cmds.Add(cmd);
                }
            }
        }
        public static void Execute()
        {
            context.ReadinputByFile(null);
            if (!_pringArgs)
            {
                context.PrintArgs(); _pringArgs = true;
            }
            if (!getBuildEnabled())
            {
                return;
            }
            getProcessors();
            doExecute();
        }
        public static void Execute(string args)
        {
            context.ReadInputByString(args);
            if (!_pringArgs)
            {
                context.PrintArgs(); _pringArgs = true;
            }
            if (!getBuildEnabled())
            {
                return;
            }
            doExecute();
        }
        private static void doExecute()
        {
            updateSetting();
            foreach (var item in _cmds)
            {
                item.BuildStart(context);
            }
            if (context.GetBoolArg("BuildBundle"))
            {
                BuildBundle();
            }
            if (context.HasError)
            {
                return;
            }
            if (context.GetBoolArg("BuildProject"))
            {
                ExportProject();
            }

        }


        private static void BuildBundle()
        {
            foreach (var item in _cmds)
            {
                item.BuildBundlePrepare(context);
            }



            string pathExportBundle = context.GetStringArg("PathExportBundle").Trim();
            if (string.IsNullOrEmpty(pathExportBundle))
            {
                pathExportBundle = Path.GetFullPath(Application.dataPath + "/../output_bundle");
            }
            else
            {
                pathExportBundle = Path.GetFullPath(pathExportBundle);
            }
            try
            {
                if (Directory.Exists(pathExportBundle))
                {
                    Directory.Delete(pathExportBundle, true);
                }
                Directory.CreateDirectory(pathExportBundle);
            }

            catch (System.Exception)
            {
                context.Error();
                throw;
            }
            if (!Directory.Exists(pathExportBundle))
            {
                Debug.LogErrorFormat(" @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ PathExportBundle not exist {0}", pathExportBundle);
                return;
            }
            context.SetBuildPath(pathExportBundle);
            var config = BundleConfig.GetInstance();
            if (config == null)
            {
                return;
            }
            config.OutputDirectory = context.BuildPath;
            config.BuildTarget = context.BuildTarget;
            config.Save();

            foreach (var item in _cmds)
            {
                item.BuildBundleBefore(context);
            }
            if (EditorUserBuildSettings.activeBuildTarget != context.BuildTarget)
            {
                var switchSuccess = EditorUserBuildSettings.SwitchActiveBuildTarget(context.BuildTargetGroup, context.BuildTarget);
                if (!switchSuccess)
                {
                    Debug.LogErrorFormat(" @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ SwitchActiveBuildTarget {0} fail", context.BuildTarget.ToString());
                    context.Error();
                    return;
                }
            }
            var p = new BuildPipline();
            if (!p.Execute())
            {
                Debug.LogErrorFormat(" @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ AssetBundleBuilder Build has error");
                context.Reset();
                return;
            }
            foreach (var item in _cmds)
            {
                item.BuildBundleAfter(context);
            }
        }
        private static void ExportProject()
        {
            getProcessors();
            try
            {
                foreach (var item in _cmds)
                {
                    item.ExportProjectPrepare(context);
                }
            }
            catch (System.Exception)
            {
                throw;
            }
            string pathExportProject = context.GetStringArg("PathExportProject").Trim();
            if (string.IsNullOrEmpty(pathExportProject))
            {
                pathExportProject = Path.GetFullPath(Application.dataPath + "/../output_project");

            }
            else
            {
                pathExportProject = Path.GetFullPath(pathExportProject);
            }
            try
            {
                if (Directory.Exists(pathExportProject))
                {
                    Directory.Delete(pathExportProject, true);
                }
                Directory.CreateDirectory(pathExportProject);
            }
            catch (System.Exception)
            {
                context.Error();
                throw;
            }

            if (!Directory.Exists(pathExportProject))
            {
                Debug.LogErrorFormat(" @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ PathExportProject not exist {0}", pathExportProject);
                return;
            }
            context.SetBuildPath(pathExportProject);

            try
            {
                foreach (var item in _cmds)
                {
                    item.ExportProjectBefore(context);
                }
            }
            catch (System.Exception)
            {
                context.Error();
                throw;
            }


            List<EditorBuildSettingsScene> levels = new List<EditorBuildSettingsScene>();
            string buildScenes = context.GetStringArg("BuildScenes");
            if (!string.IsNullOrEmpty(buildScenes))
            {
                var sceneMap = new Dictionary<string, EditorBuildSettingsScene>();
                foreach (var scene in EditorBuildSettings.scenes)
                {
                    sceneMap.Add(scene.path.ToLower(), scene);
                }
                var list = buildScenes.Split(',');
                foreach (var scenePath in list)
                {
                    if (sceneMap.TryGetValue(scenePath.ToLower(), out EditorBuildSettingsScene scene))
                    {
                        levels.Add(scene);
                        scene.enabled = true;
                    }
                }
            }
            else
            {
                foreach (var scene in EditorBuildSettings.scenes)
                {
                    if (scene.enabled)
                    {
                        levels.Add(scene);
                    }
                }
            }

            if (levels.Count < 1)
            {
                Debug.LogErrorFormat(" @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ BuildScenes not set");
                context.Error();
                return;
            }

            if (EditorUserBuildSettings.activeBuildTarget != context.BuildTarget)
            {
                var switchSuccess = EditorUserBuildSettings.SwitchActiveBuildTarget(context.BuildTargetGroup, context.BuildTarget);
                if (!switchSuccess)
                {
                    Debug.LogErrorFormat(" @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ SwitchActiveBuildTarget {0} fail", context.BuildTarget.ToString());
                    context.Error();
                    return;
                }
            }

            try
            {
                var LocationName = context.GetLocationPathName();
                Debug.LogErrorFormat("export path [{0}]", LocationName);
                var report = BuildPipeline.BuildPlayer(levels.ToArray(), LocationName, context.BuildTarget, context.BuildOptions);
                if (report.summary.result == UnityEditor.Build.Reporting.BuildResult.Succeeded)
                {
                    Debug.LogErrorFormat("export success! [{0}]", context.BuildPath);
                }
                else
                {
                    Debug.LogErrorFormat(" @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ export error! [{0}]", context.BuildPath);
                }
            }
            catch (System.Exception)
            {
                context.Error();
                throw;
            }
            try
            {
                foreach (var item in _cmds)
                {
                    item.ExportProjectAfter(context);
                }
            }
            catch (System.Exception)
            {

                throw;
            }

        }
        private static bool getBuildEnabled()
        {
            var buildTargetStr = context.GetStringArg("BuildTarget");
            BuildTarget target = BuildTarget.NoTarget;
            if (!string.IsNullOrEmpty(buildTargetStr))
            {
                if (Enum.TryParse<BuildTarget>(buildTargetStr, out BuildTarget v))
                {
                    target = v;
                }
            }
            if (target == BuildTarget.NoTarget)
            {
                Debug.LogErrorFormat(" @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ BuildTarget not set");
                return false;
            }

            var BuildTargetGroupStr = context.GetStringArg("BuildTargetGroup");
            BuildTargetGroup targetGroup = BuildTargetGroup.Unknown;
            if (!string.IsNullOrEmpty(BuildTargetGroupStr))
            {
                if (Enum.TryParse<BuildTargetGroup>(buildTargetStr, out BuildTargetGroup v))
                {
                    targetGroup = v;
                }
            }
            if (targetGroup == BuildTargetGroup.Unknown)
            {
                targetGroup = BuildPipeline.GetBuildTargetGroup(target);
            }
            if (targetGroup == BuildTargetGroup.Unknown)
            {
                Debug.LogErrorFormat(" @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ BuildTargetGroup not set");
                return false;
            }
            if (!BuildPipeline.IsBuildTargetSupported(targetGroup, target))
            {
                Debug.LogErrorFormat(" @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ not Supported BuildTargetGroup: {0} BuildTarget: {1}", targetGroup.ToString(), target.ToString());
                return false;
            }
            context.SetTarget(targetGroup, target);
            var defines = context.GetStringArg("ScriptingDefines");
            if (!string.IsNullOrEmpty(defines))
            {
                var list = defines.Split(',');
                foreach (var item in list)
                {
                    context.AddDefineSymbols(item);
                }
            }
            return true;
        }
        private static void updateSetting()
        {
            var productName = context.GetStringArg("productName").Trim();
            if (string.IsNullOrEmpty(productName))
            {
                productName = PlayerSettings.productName;
                if (string.IsNullOrEmpty(productName))
                {
                    productName = "GameApp";
                }
            }
            PlayerSettings.productName = productName;

            var companyName = context.GetStringArg("companyName").Trim();
            if (string.IsNullOrEmpty(companyName))
            {
                companyName = PlayerSettings.companyName;
                if (string.IsNullOrEmpty(companyName))
                {
                    companyName = "GameApp";
                }
            }
            PlayerSettings.companyName = companyName;

            var bundleVersion = context.GetStringArg("bundleVersion").Trim();
            if (string.IsNullOrEmpty(companyName))
            {
                bundleVersion = PlayerSettings.bundleVersion;
                if (string.IsNullOrEmpty(bundleVersion))
                {
                    bundleVersion = "1.0.0";
                }
            }
            PlayerSettings.bundleVersion = bundleVersion;

            var bundleVersionCode = context.GetStringArg("bundleVersionCode").Trim();
            if (!string.IsNullOrEmpty(bundleVersionCode))
            {
                if (int.TryParse(bundleVersionCode, out var versionCode))
                {
                    PlayerSettings.Android.bundleVersionCode = versionCode;
                }
            }


            var Identifier = context.GetStringArg("Identifier").Trim();
            if (string.IsNullOrEmpty(Identifier))
            {
                Identifier = PlayerSettings.GetApplicationIdentifier(context.BuildTargetGroup);
                if (string.IsNullOrEmpty(Identifier))
                {
                    Identifier = "net.lovedna.game";
                }
            }


#if UNITY_2022_2_OR_NEWER
            PlayerSettings.SetApplicationIdentifier(UnityEditor.Build.NamedBuildTarget.FromBuildTargetGroup(context.BuildTargetGroup), Identifier);
#else
            PlayerSettings.SetApplicationIdentifier(context.BuildTargetGroup, Identifier);
#endif

            EditorUserBuildSettings.exportAsGoogleAndroidProject = true;

            ScriptingImplementation scriptBackend = ScriptingImplementation.Mono2x;
            var backend = context.GetStringArg("ScriptingBackend", "Mono2x").ToLower();
            AndroidArchitecture Architecture = AndroidArchitecture.None;
            string architecture = context.GetStringArg("AndroidArchitecture", AndroidArchitecture.ARMv7.ToString().ToLower()).ToLower();
            if (backend.Contains(ScriptingImplementation.Mono2x.ToString().ToLower()))
            {
                scriptBackend = ScriptingImplementation.Mono2x;
            }
            else if (backend.Contains(ScriptingImplementation.IL2CPP.ToString().ToLower()))
            {
                scriptBackend = ScriptingImplementation.IL2CPP;
            }
            else if (backend.Contains(ScriptingImplementation.WinRTDotNET.ToString().ToLower()))
            {
                scriptBackend = ScriptingImplementation.WinRTDotNET;
            }

            if (architecture.Contains(AndroidArchitecture.All.ToString().ToLower()))
            {
                Architecture = AndroidArchitecture.All;
                scriptBackend = ScriptingImplementation.IL2CPP;
            }
            else
            {
                Architecture = AndroidArchitecture.None;
                if (architecture.Contains(AndroidArchitecture.ARMv7.ToString().ToLower()))
                {
                    Architecture |= AndroidArchitecture.ARMv7;
                }
                if (architecture.Contains(AndroidArchitecture.ARM64.ToString().ToLower()))
                {
                    Architecture |= AndroidArchitecture.ARM64;
                    scriptBackend = ScriptingImplementation.IL2CPP;
                }
                if (architecture.Contains(AndroidArchitecture.X86.ToString().ToLower()))
                {
                    Architecture |= AndroidArchitecture.X86;
                }
                if (architecture.Contains(AndroidArchitecture.X86_64.ToString().ToLower()))
                {
                    Architecture |= AndroidArchitecture.X86_64;
                    scriptBackend = ScriptingImplementation.IL2CPP;
                }
            }

            PlayerSettings.Android.targetArchitectures = Architecture;
            PlayerSettings.SetScriptingBackend(context.BuildTargetGroup, scriptBackend);

            context.ApplyDefineSymbols();
            context.ApplyKeystore();


            BuildOptions options = BuildOptions.None;
            options |= BuildOptions.CompressWithLz4;
            if (context.GetBoolArg("Development"))
            {
                // adb forward tcp:34999 localabstract:Unity-包名
                options |= BuildOptions.Development;
                options |= BuildOptions.EnableDeepProfilingSupport;
                options |= BuildOptions.AllowDebugging;
            }
            context.BuildOptions = options;
            context.Apk = context.GetBoolArg("Apk");
        }
    }
    public interface IBuildCommandProcessor
    {
        void BuildStart(BuildCommandContext context);
        void ExportProjectPrepare(BuildCommandContext context);
        void ExportProjectBefore(BuildCommandContext context);
        void ExportProjectAfter(BuildCommandContext context);
        void BuildBundlePrepare(BuildCommandContext context);
        void BuildBundleBefore(BuildCommandContext context);
        void BuildBundleAfter(BuildCommandContext context);
    }
    public class BuildCommandProcessor : IBuildCommandProcessor
    {
        public BuildCommandProcessor()
        {

        }
        public virtual void BuildStart(BuildCommandContext context)
        {

        }
        public virtual void ExportProjectPrepare(BuildCommandContext context)
        {

        }
        public virtual void BuildBundleBefore(BuildCommandContext context)
        {
        }

        public virtual void ExportProjectBefore(BuildCommandContext context)
        {
        }

        public virtual void BuildBundlePrepare(BuildCommandContext context)
        {
        }

        public virtual void ExportProjectAfter(BuildCommandContext context)
        {
        }

        public virtual void BuildBundleAfter(BuildCommandContext context)
        {
        }
    }
    public class BuildCommandContext
    {
        public bool HasError { get; private set; }
        public List<string> ScriptingDefines { get; private set; }
        //NamedBuildTarget is future
        public BuildTargetGroup BuildTargetGroup { get; private set; }
        public BuildTarget BuildTarget { get; private set; }
        public BuildOptions BuildOptions;
        public bool Apk;
        public string BuildPath { get; private set; }
        public string LocationName { get; private set; }
        private List<string> editorScriptingDefines = new List<string>();
        private Dictionary<string, string> _args = new Dictionary<string, string>();
        public BuildCommandContext()
        {
            ScriptingDefines = new List<string>();
            BuildOptions = BuildOptions.None;
            BuildTargetGroup = BuildTargetGroup.Unknown;
            BuildTarget = BuildTarget.NoTarget;
            HasError = false;
            LocationName = string.Empty;
        }
        public void SetLocationName(string value)
        {
            LocationName = value;
        }
        public string GetLocationPathName()
        {
            if (string.IsNullOrEmpty(LocationName))
            {
                if (BuildTarget == UnityEditor.BuildTarget.StandaloneWindows)
                {
                    SetLocationName(PlayerSettings.productName + ".exe");
                }
                else if (BuildTarget == UnityEditor.BuildTarget.StandaloneWindows64)
                {
                    SetLocationName(PlayerSettings.productName + ".exe");
                }
            }
            if (BuildTarget == UnityEditor.BuildTarget.Android)
            {
                if (Apk || !Directory.Exists(BuildPath))
                {
                    EditorUserBuildSettings.exportAsGoogleAndroidProject = false;
                    SetLocationName(PlayerSettings.productName + ".apk");
                }
                else
                {
                    EditorUserBuildSettings.exportAsGoogleAndroidProject = true;
                }
            }
            return Path.Combine(BuildPath, LocationName);
        }
        public void SetTarget(BuildTargetGroup group, BuildTarget target)
        {
            BuildTargetGroup = group;
            BuildTarget = target;

            Debug.LogErrorFormat("##### set targetGroup: {0},target: {1}", BuildTargetGroup, BuildTarget);

            editorScriptingDefines.Clear();
#if UNITY_2022_2_OR_NEWER
            var defines = PlayerSettings.GetScriptingDefineSymbols(UnityEditor.Build.NamedBuildTarget.FromBuildTargetGroup(BuildTargetGroup));
#else
            var defines = PlayerSettings.GetScriptingDefineSymbolsForGroup(BuildTargetGroup);
#endif

            var list = defines.Split(';');
            foreach (var item in list)
            {
                var defName = item.Trim();
                if (!editorScriptingDefines.Contains(defName))
                {
                    editorScriptingDefines.Add(defName);
                }
            }
        }
        public void SetBuildPath(string value)
        {
            BuildPath = value;
            Debug.LogErrorFormat("##### set buildPath: {0}", value);
        }
        public void ReadInputByString(string value)
        {
            if (string.IsNullOrEmpty(value)) return;
            //k=v;k=v;
            _args.Clear();
            BuildTarget = BuildTarget.NoTarget;
            //外部设置的参数
            string[] args = value.Split(';');
            foreach (var kvs in args)
            {
                string k, v;
                if (parseKeyValue(kvs, out k, out v))
                {
                    SetArg(k, v);
                }
            }
            ReadBuildSettings();
        }
        public void ReadinputByFile(string path)
        {
            _args.Clear();
            BuildTarget = BuildTarget.NoTarget;
            if (string.IsNullOrEmpty(path) || !File.Exists(path))
            {
                path = Path.GetFullPath(Path.Combine(Application.dataPath, "../SettingsInput.txt"));
                if (!File.Exists(path))
                {
                    return;
                }
            }
            var lines = File.ReadAllLines(path);
            var len = lines.Length;
            while (len-- > 0)
            {
                string k, v;
                if (parseKeyValue(lines[len], out k, out v))
                {
                    SetArg(k, v);
                }
            }
            ReadBuildSettings();
        }
        private void ReadBuildSettings()
        {
            //本地设置，不常变化的参数
            var buildSettingsPath = GetStringArg("SettingsBuild");
            if (string.IsNullOrEmpty(buildSettingsPath))
            {
                buildSettingsPath = Path.GetFullPath(Path.Combine(Application.dataPath, "../SettingsBuild.txt"));
            }
            if (File.Exists(buildSettingsPath))
            {
                //是否替换本地设置
                bool overrideSetting = GetBoolArg("override");

                //先设置本地参数
                var lines = File.ReadAllLines(buildSettingsPath);
                var len = lines.Length;
                while (len-- > 0)
                {
                    string k, v;
                    if (parseKeyValue(lines[len], out k, out v))
                    {
                        if (overrideSetting && !string.IsNullOrEmpty(GetStringArg(k)))
                        {
                            //如果已经有参数并且是替换模式，则不保存本地设置
                            continue;
                        }
                        SetArg(k, v);
                    }
                }
            }
        }
        public void PrintArgs()
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.AppendLine(" ##################################### args=");
            stringBuilder.AppendLine("[");
            foreach (var item in _args)
            {
                stringBuilder.AppendLine(string.Format("{0}:{1}", item.Key, item.Value));
            }
            stringBuilder.AppendLine("]");
            stringBuilder.AppendLine(" #####################################");
            Debug.LogError(stringBuilder.ToString());
        }
        private bool parseKeyValue(string line, out string key, out string value)
        {
            key = string.Empty;
            value = string.Empty;
            if (string.IsNullOrEmpty(line.Trim()))
            {
                return false;
            }
            if (line.StartsWith("#"))
            {
                return false;
            }
            if (line.IndexOf("=") == -1)
            {
                return false;
            }
            var kv = line.Split('=');
            if (kv.Length < 2)
            {
                return false;
            }
            string k = kv[0].Trim().ToLower();
            string v = kv[1];
            if (string.IsNullOrEmpty(k))
            {
                return false;
            }
            key = k;
            value = v;
            return true;
        }
        public string GetStringArg(string name, string defaultValue = "")
        {
            if (_args.TryGetValue(name.ToLower(), out string value))
            {
                defaultValue = value;
            }
            return defaultValue;
        }
        public bool GetBoolArg(string name)
        {
            var value = GetStringArg(name, "0").Trim().ToLower();
            return value == "true" || value == "1";
        }
        public int GetIntArg(string name, int defaultValue = 0)
        {
            var value = GetStringArg(name, defaultValue.ToString()).Trim().ToLower();
            if (int.TryParse(value, out int result))
            {
                return result;
            }
            return defaultValue;
        }
        public bool ExistArg(string name)
        {
            return _args.ContainsKey(name.ToLower());
        }
        public void SetArg(string name, string value)
        {
            name = name.Trim().ToLower();
            value = value.Trim();
            if (_args.ContainsKey(name))
            {
                if (string.IsNullOrEmpty(value))
                {
                    _args.Remove(name);
                }
                {
                    _args[name] = value;
                }
            }
            else
            {
                _args.Add(name, value);
            }
        }
        public void ClearDefines()
        {
            ScriptingDefines.Clear();
        }
        public void AddDefineSymbols(string value)
        {
            if (string.IsNullOrEmpty(value) || string.IsNullOrEmpty(value.Trim())) return;
            if (!ScriptingDefines.Contains(value))
            {
                ScriptingDefines.Add(value);
            }
        }
        public void Error()
        {
            Reset();
            HasError = true;
        }
        public void Reset()
        {
            ApplyDefineSymbols(true);
        }
        public void ApplyDefineSymbols(bool revert = false)
        {
            var map = new Dictionary<string, bool>();
            foreach (var item in editorScriptingDefines)
            {
                if (!map.ContainsKey(item))
                {
                    map.Add(item, true);
                }
            }
            foreach (var item in ScriptingDefines)
            {
                if (!map.ContainsKey(item))
                {
                    map.Add(item, true);
                }
            }
            //宏定义
            var list = new List<string>();
            foreach (var item in map)
            {
                list.Add(item.Key);
            }
#if UNITY_2022_2_OR_NEWER
            PlayerSettings.SetScriptingDefineSymbols(UnityEditor.Build.NamedBuildTarget.FromBuildTargetGroup(BuildTargetGroup), list.ToArray());
#else
            PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup, list.ToArray());
#endif
        }
        public void ApplyKeystore()
        {
            //android 证书
            PlayerSettings.Android.useCustomKeystore = false;
            var keystoreFile = GetStringArg("keystoreFile").Trim();
            if (string.IsNullOrEmpty(keystoreFile))
            {
                return;
            }
            keystoreFile = Path.GetFullPath(keystoreFile);
            if (!File.Exists(keystoreFile))
            {
                //在项目中找证书
                keystoreFile = Path.Combine(Application.dataPath, keystoreFile);
                if (!File.Exists(keystoreFile))
                {
                    return;
                }
            }
            var keystorePass = GetStringArg("keystorePass").Trim();
            var keyaliasName = GetStringArg("keyaliasName").Trim();
            var keyaliasPass = GetStringArg("keyaliasPass").Trim();
            if (string.IsNullOrEmpty(keystorePass) || string.IsNullOrEmpty(keyaliasName) || string.IsNullOrEmpty(keyaliasPass))
            {
                return;
            }
            PlayerSettings.Android.useCustomKeystore = true;
            PlayerSettings.Android.keystoreName = keystoreFile;
            PlayerSettings.Android.keystorePass = keystorePass;
            PlayerSettings.Android.keyaliasName = keyaliasName;
            PlayerSettings.Android.keyaliasPass = keyaliasPass;
        }
    }
}